package com.bootdo.app.service.impl;

import com.bootdo.app.dao.AppSellerDao;
import com.bootdo.app.exception.AppException;
import com.bootdo.app.exception.RCode;
import com.bootdo.app.htmlVO.customer.MyUserInfo;
import com.bootdo.app.htmlVO.seller.SellerAndImages;
import com.bootdo.app.service.AppSellerService;
import com.bootdo.app.service.AppUserInfoService;
import com.bootdo.app.utils.AppUtilXML;
import com.bootdo.app.utils.FileAppUtils;
import com.bootdo.app.utils.returnresponse.ServerResponse;
import com.bootdo.appsystem.domain.ImageinfoDO;
import com.bootdo.appsystem.domain.ProducesinfoDO;
import com.bootdo.appsystem.domain.SellerDO;
import com.bootdo.appsystem.domain.UseraccountDO;
import com.bootdo.appsystem.service.ImageinfoService;
import com.bootdo.appsystem.service.ProducesinfoService;
import com.bootdo.appsystem.service.SellerService;
import com.bootdo.appsystem.service.UseraccountService;
import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.utils.FileType;
import com.bootdo.common.utils.FileUtil;
import com.bootdo.common.utils.HttpContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 【@author 】 @fan
 * 【@Email  】 mooncluyed@outlook.com
 * 【类解释  】
 * 【@Date   】 2018/5/14 0014
 **/
@Service
public class AppSellerServiceImpl implements AppSellerService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BootdoConfig bootdoConfig;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private ImageinfoService imageinfoService;
    @Autowired
    private ProducesinfoService producesinfoService;

    @Override
    public SellerDO getSessionSellerDO() {
        // 获取到当前登录会员的商家所有信息
        UseraccountDO useraccountDO = (UseraccountDO) HttpContextUtils.getSessionObject(AppUtilXML.SESSION_CUSTOMER);
        HashMap<String , Object> map = new HashMap<>();
        map.put("uid" , useraccountDO.getUid());
        List<SellerDO> sellerDOList = sellerService.list(map);
        if(sellerDOList.size() != 1){
            throw new AppException(RCode.NEED_LOGIN);
        }
        SellerDO sellerDO = sellerDOList.get(0);
        return sellerDO;
    }

    @Override
    public FileDO uploadSellerImage(MultipartFile file,UseraccountDO useraccountDO) {
        String fileName = file.getOriginalFilename();
        fileName = FileAppUtils.toBlobToPng(fileName);
        fileName = FileUtil.renameToUUID(fileName);
        //创建新的文件夹
        //创建格式：/files/seller/seller1/20180514/201805149f751de8-2707-42d3-891c-b888c1f360c8.jpg
        File file2 = new File(bootdoConfig.getUploadPath()+"/seller/" + "uid" + useraccountDO.getUid() + "/" + new SimpleDateFormat("yyyyMMdd").format(new Date()));
        FileDO sysFile = new FileDO(FileType.fileType(fileName), "/files/seller/" + "uid"+ useraccountDO.getUid() + "/" + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/"+ file2.getName()+fileName, new Date());
        try {
            FileUtil.uploadFile(file.getBytes(), file2.getPath()+ "\\" , file2.getName()+fileName);
        } catch (Exception e) {
            logger.error("【上传接口出现错误】" + e.getMessage());
            throw new AppException(RCode.FANHUINULL);
        }
        return sysFile;
    }

    @Override
    public FileDO uploadEvaluateImage(MultipartFile file,UseraccountDO useraccountDO) {
        String fileName = file.getOriginalFilename();
        fileName = FileAppUtils.toBlobToPng(fileName);
        fileName = FileUtil.renameToUUID(fileName);
        //创建新的文件夹
        //创建格式：/files/seller/seller1/20180514/201805149f751de8-2707-42d3-891c-b888c1f360c8.jpg
        File file2 = new File(bootdoConfig.getUploadPath()+"/evaluate/" + "uid" + useraccountDO.getUid() + "/" + new SimpleDateFormat("yyyyMMdd").format(new Date()));
        FileDO sysFile = new FileDO(FileType.fileType(fileName), "/files/evaluate/" + "uid"+ useraccountDO.getUid() + "/" + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/"+ file2.getName()+fileName, new Date());
        try {
            FileUtil.uploadFile(file.getBytes(), file2.getPath()+ "\\" , file2.getName()+fileName);
        } catch (Exception e) {
            logger.error("【上传接口出现错误】" + e.getMessage());
            throw new AppException(RCode.FANHUINULL);
        }
        return sysFile;
    }

    @Override
    public SellerAndImages getSellerAndImages(Integer sellerId) {

        SellerDO sellerDO = sellerService.get(sellerId);
        System.out.println("【商家信息】" + sellerDO);
        HashMap<String , Object> map = new HashMap<>();
//        获取商家的所有图片
        map.put("allid",sellerDO.getUid());
//        2、代表着商家商铺上传的图片
        map.put("type",10006);
//        图片的状态
        map.put("imagetype",1);
        map.put("sort","display");
        map.put("sort","imageid");
        map.put("order","asc");
        List<ImageinfoDO> imageinfoDOList = imageinfoService.list(map);
//        商家所有商品
        HashMap<String , Object> mapPro = new HashMap<>();
        mapPro.put("sellerid",sellerDO.getSellerid());
        List<ProducesinfoDO> goodsList = producesinfoService.list(mapPro);


//        将数据存放起来
        SellerAndImages sellerAndImages = new SellerAndImages();
        sellerAndImages.setSellerDO(sellerDO);
        sellerAndImages.setImageinfoDO(imageinfoDOList);
        sellerAndImages.setProducesinfoDOList(goodsList);

        return sellerAndImages;
    }




}
